package org.lboot.idmo.controller;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
import cn.hutool.core.util.IdUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.lboot.core.constant.CommonConst;
import org.lboot.core.domain.ErrMsg;
import org.lboot.core.domain.ResponseDTO;
import org.lboot.idmo.module.role.SysRole;
import org.lboot.idmo.module.user.SysUser;
import org.lboot.idmo.module.user.SysUserService;
import org.lboot.idmo.module.user.role.SysUserRoleService;
import org.lboot.idmo.params.LoginParams;
import org.lboot.llog.anno.ApiLog;
import org.springframework.http.HttpStatus;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

@Slf4j
@RestController
@Api(tags = "登录鉴权")
@RequestMapping("idm")
@AllArgsConstructor
public class IdmoAuthController {

    SysUserService userService;

    SysUserRoleService userRoleService;

    @GetMapping(value = "captcha")
    @ApiOperation(value = "获取图片验证码",notes = "")
    public ResponseDTO<Map<String,String>> getCaptcha(){
        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);
        log.info("生成的图片验证码:"+lineCaptcha.getCode());
        // 生成和验证码对应的唯一UUID
        String uuid = IdUtil.randomUUID();
        Map<String,String> captchaMap = new HashMap<>(2);
        captchaMap.put("uuid",CommonConst.CAPTCHA_CODE_KEY.concat(uuid));
        captchaMap.put("img",lineCaptcha.getImageBase64Data());
        return ResponseDTO.succData(captchaMap);
    }

    @PostMapping("login")
    @ApiLog(value = "系统鉴权",ignoreRequest = 1)
    @ApiOperation(value = "用户登录",notes = "")
    public ResponseDTO<Object> login(@RequestBody @Validated LoginParams loginParams){
        Optional<SysUser> userOp = userService.getByUsername(loginParams.getUsername());
        if (!userOp.isPresent()) {
            return ResponseDTO.wrap(HttpStatus.BAD_REQUEST, ErrMsg.build("请检查账号是否正确"));
        }
        SysUser user = userOp.get();
        // 比对密码
        if (!loginParams.getPassword().equals(user.getPassword())){
            return ResponseDTO.wrap(HttpStatus.BAD_REQUEST,ErrMsg.build("请检查账号密码是否正确"));
        }
       StpUtil.login(user.getId());
       String token = StpUtil.getTokenValue();
       Map<String, Object> resultMap = new HashMap<>(6);
       List<SysRole> roles = userRoleService.getRoles(user.getId());
       List<String> roleNames = roles.stream().map(SysRole::getRoleName).collect(Collectors.toList());
       resultMap.put("accessToken",token);
       resultMap.put("token",token);
       resultMap.put("username", user.getUsername());
       resultMap.put("nickname", user.getNickname());
       resultMap.put("roles", roleNames);
       return ResponseDTO.succData(resultMap);
    }

    @PostMapping("logout")
    @ApiOperation(value = "用户登出")
    public ResponseDTO<Object> logout(){
        StpUtil.logout();
        return ResponseDTO.succ();
    }
}
